package com.software.cool.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author diaojianfeng
 * @date 2019/5/27 下午5:01
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

	/**
	 * API controller包路
	 */
	private final static String BASE_PACKAGE = "com.software.online.edu";

	/**
	 * API页面标题
	 */
	private final static  String TITLE = "AI智学管理系统";

	/**
	 * API描述
	 */
	private final static String DESCRIPTION = "";

	/**
	 * 版本号
	 */
	private final static String VERSION = "1.0";

	/**
	 * 联系人
	 */
	private final static Contact CONTACT = new Contact("darren",
			"http://xxxxxxxx",
			"xxxxx@qq.com");

	@Value("${swagger.host}")
	private String host;

	@Bean
	public Docket api() {
		List<Parameter> aParameters = new ArrayList<Parameter>();
		ParameterBuilder tokenPar1 = new ParameterBuilder();
		tokenPar1
				//参数类型支持header, cookie, body, query etc
				.parameterType("header")
				//指定参数值的类型
				.modelRef(new ModelRef("string"))
				.required(false).build();
		aParameters.add(tokenPar1.build());
		return new Docket(DocumentationType.SWAGGER_2).host(host).select()
				.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
				.paths(PathSelectors.any())
				.build().apiInfo(apiEndPointsInfo());
	}

	private ApiInfo apiEndPointsInfo() {
		return new ApiInfoBuilder().title(TITLE)
				.description(DESCRIPTION)
				.contact(CONTACT)
				.version(VERSION)
				.build();
	}
}
